﻿#region
/*******************************************************************************
** File Name:		    NewsService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;
using System.Collections;

namespace Phoenix.Service
{
    public class NewsService : INewsService
    {
        INews news = new NewsRepository();
        INewsCatalogService newsCatalogService = new NewsCatalogService();

        #region  Auto Generation

        public void AddNews(NewsModel model)
        {
            ENews entity = ConvertModelToEntity(model);

            news.AddNews(entity);
        }

        public void UpdateByNewsID(NewsModel model)
        {
            ENews entity = ConvertModelToEntity(model);

            news.UpdateByNewsID(entity);

        }

        public void DeleteByNewsID(System.Int32 newsID)
        {

            news.DeleteByNewsID(newsID);

        }

        public NewsModel GetByNewsID(System.Int32 newsID)
        {

            ENews entity = news.GetByNewsID(newsID);

            NewsModel model = ConvertEntityToModel(entity);


            return model;
        }

        public NewsModel ConvertEntityToModel(ENews entity)
        {
            if (entity == null)
            {
                return null;
            }
            NewsModel model = new NewsModel();
            model.NewsID = entity.NewsID;
            model.CatalogID = entity.CatalogID;
            model.Title = entity.Title;
            model.Keywords = entity.Keywords;
            model.Description = entity.Description;
            model.Content = entity.Content;
            model.DisplayFlag = entity.DisplayFlag;
            model.VisitCount = entity.VisitCount;
            model.SortOrder = entity.SortOrder;
            model.CreateTime = entity.CreateTime;
            model.CreateUser = entity.CreateUser;
            model.UpdateTime = entity.UpdateTime;
            model.UpdateUser = entity.UpdateUser;
            return model;
        }

        public ENews ConvertModelToEntity(NewsModel model)
        {
            if (model == null)
            {
                return null;
            }
            ENews entity = new ENews();
            entity.NewsID = model.NewsID;
            entity.CatalogID = model.CatalogID;
            entity.Title = model.Title;
            entity.Keywords = model.Keywords;
            entity.Description = model.Description;
            entity.Content = model.Content;
            entity.DisplayFlag = model.DisplayFlag;
            entity.VisitCount = model.VisitCount;
            entity.SortOrder = model.SortOrder;
            entity.CreateTime = model.CreateTime;
            entity.CreateUser = model.CreateUser;
            entity.UpdateTime = model.UpdateTime;
            entity.UpdateUser = model.UpdateUser;
            return entity;
        }


        #endregion

        public IList<NewsModel> GetPagedList(int pageSize, int pageIndex, int categoryID, out int totalRecord)
		{
			IList<ENews> entityList = news.GetPagedList(pageSize, pageIndex, categoryID, out totalRecord);
			if (entityList == null)
				return null;

            IList<NewsCatalogModel> newsCatalogList = newsCatalogService.GetByParentID(0);

            Hashtable htCatalog =newsCatalogService.GetIdNameByParentID(0);

			IList<NewsModel> modelList = new List<NewsModel>();

			foreach (var item in entityList)
			{
                NewsModel model = ConvertEntityToModel(item);

                if(model!=null)
                  {
                    model.CatalogName=htCatalog[model.CatalogID].ToString();
                    modelList.Add(model);
                  }

				
			}
			
			return modelList;

		}

    }
}